var $dataChart;

require(['grid', 'topBar', 'layout','date'], function () {

    var postData;

    $('#top').topBar({       
        left: [{
                   type: 'filter-more',
                   dateType:'custom',
                   search: function () {
                	   if(getPostData()){
		                   $('#grid').grid('setGridParam', {
                               page:1,
		                       datatype: "json",
		                       postData: postData
		                   }).trigger("reloadGrid");
                	   }
                   },
                   item: [
                       {
                           text: '时间快捷',
                           name1: 'beginTime',
                           name2: 'endTime',
                           type: 'dateLine'
                       }
                   ]
               }
        ],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                        	exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                        	exportFile('Pdf');
                        }
                    }
                ]
            },
            {
                text: '电耗里程分析',
                type: 'btn',
                click: function () {
                    var nodes = commonFun.getLeftTreeCheckedNodes("CAR");
                    if(!nodes){
                        $.messager.alert("提示", "请选择查询车辆。", "info");
                        return;
                    }
                    if(nodes.length>1) {
                        $.messager.alert("提示","只能选择一辆车。","info");
                        return;
                    }
					var data = $('#top').parent().getValues();
                    data.carId=nodes[0];
					if(!$dataChart){						
						$dataChart=$('<div>').dialog({
		            	        title:'电耗里程分析',
		                        url: './mainte/report/viewDataChart.htm',
		            	        height: 550,
		            	        width: 720,
		            	        buttons:[
		        	                 {
		        	                     text: '关闭',
		        	                     css: 'btn-cancel',
		        	                     click: function () {
		        	                    	 $(this).dialog('close');
		        	                     }
		        	                 }
		                         ]
		                    });
					}
                	$dataChart.dialog('open');
                	$dataChart.dialog('exec', 'chargingMileAnaly',data);
                }
            }
        ]
    });

    $('body').layout();

    function exportFile(suffix){
    	if(getPostData()){
    		if($('#grid').getGridParam("reccount")>0){
	    		var url = "/chargingMileAnalysis/exportChargingMileAnalysis"+suffix+".htm";
				$.exportFile(url,postData);
    		}
    		else{
    			$.messager.alert("提示","请先查询数据！","info");
    		}
	    }
    }
    
  //获取左侧选择的节点信息
	function getPostData(){
        var rcx = commonFun.getTreeCheckNodeToArgs();
        if (rcx) {
            postData = $.extend(rcx, $("#top").parent().getValues());
        }
        return rcx;
	}

    $('#grid').grid({
    	url : "/chargingMileAnalysis/searchChargingMileAnalysis.htm",
        multiselect:false,
		colModel:[
		    {name : "carId", label: "id", hidden: true},
			{name: "licPltNo", label: "车牌号", width:"80"},
			{name: "lineName", label: "运营线路", width:"80"},
			{name: "organName", label: "所属机构", width:"80"},
			{name: "bteryMaterialType", label: "电池材料类型", width:"90"},
			{name: "chargeBeginTime", label: "上次充电时间", width:"110"},
			{name: "chargeEndTime", label: "本次充电时间", width:"110"},
			{name: "lastingDetail", label: "运行时长", align: "right", width:"110"},
			{name: "preChargeEndSocValue", label: "上次充电后SOC值(%)", align: "right", width:"135"},
			{name: "chargeBeginSocValue", label: "本次充电前SOC值(%)", align: "right", width:"135"},
			{name: "socDiffValue", label: "耗电量(KWh)", align: "right", width:"110"},
			{name: "totalMile", label: "运行里程(km)", align: "right", width:"110"},
			{name: "avgSpeed", label: "平均车速(km/h)", align: "right", width:"110"},
			{name: "chargeEndSocValue", label: "本次充电后SOC值(%)", align: "right", width:"135"},
			{name: "durationMile", label: "充电后续航里程(km)", align: "right", width:"135"}]
    });
    
})
